package com.atguigu.srb.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.enums.TransTypeEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.UserAccountMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.bo.TransFlowBO;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.TransFlowService;
import com.atguigu.srb.core.service.UserAccountService;
import com.atguigu.srb.core.service.UserBindService;
import com.atguigu.srb.core.util.LendNoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户账户 服务实现类
 * </p>
 *
 * @author 桃谷六仙
 * @since 2022-12-14
 */
@Service
@Slf4j
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private TransFlowService transFlowService;
    @Resource
    private UserAccountMapper userAccountMapper;
    @Resource
    private UserBindService userBindService;
    @Resource
    private UserAccountService userAccountService;

    @Override
    public String commitCharge(BigDecimal chargeAmt, Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        String bindCode=userInfo.getBindCode();
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("agentId",HfbConst.AGENT_ID);
        paramMap.put("agentBillNo", LendNoUtils.getNo());
        paramMap.put("bindCode",bindCode);
        paramMap.put("chargeAmt",chargeAmt);
        paramMap.put("feeAmt",new BigDecimal("0"));
        paramMap.put("notifyUrl",HfbConst.RECHARGE_NOTIFY_URL);
        paramMap.put("returnUrl",HfbConst.RECHARGE_RETURN_URL);
        paramMap.put("timestamp",RequestHelper.getTimestamp());
        String sign= RequestHelper.getSign(paramMap);
        paramMap.put("sign",sign);

        return FormHelper.buildForm(HfbConst.RECHARGE_URL,paramMap);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String notify(Map<String, Object> paramMap) {
        log.info("充值成功："+ JSONObject.toJSONString(paramMap));
        //1.幂等性判断  标准 = 判断交易流水是否存在
        String agentBillNo = (String) paramMap.get("agentBillNo");//商户充值订单号
        boolean isSave=transFlowService.isSaveTransFlow(agentBillNo);
        if(isSave){
            log.warn("幂等性返回");
            return "success";
        }
        String bindCode = (String) paramMap.get("bindCode");//充值人绑定协议号
        String chargeAmt = (String) paramMap.get("chargeAmt");//充值金额
        //优化
        baseMapper.updateAccount(bindCode,new BigDecimal(chargeAmt),new BigDecimal(0));
        //增加交易流水
        TransFlowBO transFlowBO = new TransFlowBO(
                agentBillNo,
                bindCode,
                new BigDecimal(chargeAmt),
                TransTypeEnum.RECHARGE,
                "充值");
        transFlowService.saveTransFlow(transFlowBO);

        return "success";
    }

    @Override
    public BigDecimal getAccount(Long userId) {
        QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);

        UserAccount userAccount = baseMapper.selectOne(queryWrapper);

        return userAccount.getAmount();
    }

    @Override
    public String commitWithdraw(BigDecimal fetchAmt, Long userId) {
        //账号可用余额充足：当前用户余额>=当前用户提现余额
        BigDecimal amout = userAccountService.getAccount(userId);
        Assert.isTrue(amout.doubleValue()>=fetchAmt.doubleValue(), ResponseEnum.NOT_SUFFICIENT_FUNDS_ERROR);

        String bindCode = userBindService.getBindCodeByUserId(userId);
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("agentId",HfbConst.AGENT_ID);
        paramMap.put("agentBillNo",LendNoUtils.getWithdrawNo());
        paramMap.put("bindCode",bindCode);
        paramMap.put("fetchAmt",fetchAmt);
        paramMap.put("feeAmt",new BigDecimal(0));
        paramMap.put("notifyUrl",HfbConst.WITHDRAW_NOTIFY_URL);
        paramMap.put("returnUrl",HfbConst.WITHDRAW_RETURN_URL);
        paramMap.put("timestamp",RequestHelper.getTimestamp());
        String sign = RequestHelper.getSign(paramMap);
        paramMap.put("sign",sign);

        String buildForm = FormHelper.buildForm(HfbConst.WITHDRAW_URL, paramMap);
        return buildForm;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void notifyWithdraw(Map<String, Object> paramMap) {
        log.info("提现成功");
        String agentBillNo = (String) paramMap.get("agentBillNo");
        boolean result = transFlowService.isSaveTransFlow(agentBillNo);
        if(result){
            log.warn("幂等性返回");
            return;
        }
        String bindCode = (String) paramMap.get("bindCode");
        String fetchAmt = (String) paramMap.get("fetchAmt");
        //根据用户账号修改账号金额
        baseMapper.updateAccount(bindCode,new BigDecimal("-"+fetchAmt),new BigDecimal(0));
        //增加交易流水
        TransFlowBO transFlowBO = new TransFlowBO(
                agentBillNo,
                bindCode,
                new BigDecimal(fetchAmt),
                TransTypeEnum.WITHDRAW,
                "提现"
        );
        transFlowService.saveTransFlow(transFlowBO);
    }
}
